package com.weigee.ktv.presenter.adapter.rv

import android.view.LayoutInflater
import android.view.ViewGroup
import com.linde.library.adapter.SimpleViewHolder
import com.linde.library.util.DecimalPointUtil
import com.linde.library.util.ToastUtil
import com.weigee.ktv.R
import com.weigee.ktv.model.entities.ShoppingCartListEntity
import com.weigee.ktv.model.enums.ImagePreLoad
import com.weigee.ktv.model.enums.WeigeeHost
import com.weigee.ktv.model.interfaces.IShoppingCart
import kotlinx.android.synthetic.main.adapter_shopping_cart_list.view.*

/**
 * Created by linde on 2017/5/30.
 *<p/>
 */
class ShoppingCartListAdapter(val cart: IShoppingCart) : com.linde.library.adapter.BaseRvAdapter<ShoppingCartListEntity>()
{
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SimpleViewHolder
    {
        val view = LayoutInflater.from(parent?.context).inflate(R.layout.adapter_shopping_cart_list, parent, false)
        return com.linde.library.adapter.SimpleViewHolder(view)
    }

    override fun onBindViewHolder(holder: com.linde.library.adapter.SimpleViewHolder?, position: Int)
    {
        super.onBindViewHolder(holder, position)
        holder ?: return
        val v = holder.itemView
        val entity = getItem(position)

        v.tv_shopping_goods_name.text = entity?.goodsEntity?.name
        v.tv_prise.text = ("¥" + DecimalPointUtil.point(2, entity?.goodsEntity?.price ?: 0F))
        v.tv_check_num.text = entity?.shoppingGoodsCheckNum?.toString() ?: 0.toString()

        com.bumptech.glide.Glide.with(v.context).load(WeigeeHost.TEST_IMAGE.url).asBitmap().placeholder(ImagePreLoad.IMAGE.res).into(v.iv_add_to_cart)
        com.bumptech.glide.Glide.with(v.context).load(WeigeeHost.TEST_IMAGE.url).asBitmap().placeholder(ImagePreLoad.IMAGE.res).into(v.iv_sub_from_cart)
        v.iv_add_to_cart.setOnClickListener { v ->
            ToastUtil.show(v, "+")
            cart.addToCart(entity?.shopId, entity?.goodsEntity)
        }
        v.iv_sub_from_cart.setOnClickListener { v ->
            ToastUtil.show(v, "-")
            cart.removeFromCart(entity?.shopId, entity?.goodsEntity?.categoryId, entity?.goodsEntity?.id)
        }
    }
}